import javax.xml.transform.sax.SAXTransformerFactory;
import java.net.Inet4Address;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-10-05
 * Time: 20:32
 */
public class Main {
    /**
     * 统计10W个数中每个数出现的次数
     * @param array
     * @return
     */
    public static Map<Integer,Integer> count(int[] array){
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            if(map.containsKey(array[i])){
                int value=map.get(array[i]);
                map.remove(array[i]);
                map.put(array[i],value+1);
            }else{
                map.put(array[i],1);
            }
        }
        return map;
    }

    /**
     * 10W个数去重
     * @param array
     * @return
     */
    public static Set<Integer> removeDuplicates(int[]array){
        Set<Integer> set=new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            set.add(array[i]);
        }
        return set;
    }

    /**
     * 10W个数查找第一个重复的数
     * 找不到返回-1
     * @param array
     * @return
     */
    public static int findFirstDuplicate(int[]array){
        Set<Integer> set=new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            if(set.contains(array[i])){
                return array[i];
            }else{
                set.add(array[i]);
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] array=new int[100000];
        Random random=new Random();
        for (int i = 0; i < array.length; i++) {
            array[i]=random.nextInt(10000);
        }
        /*Map<Integer,Integer> map=count(array);
        System.out.println(map);*/

        /*Set<Integer> set=removeDuplicates(array);
        System.out.println(set);*/


        System.out.println(findFirstDuplicate(array));

    }
}
